unit Class_TrUtils;

interface
uses
  Windows,Classes,SysUtils,ElTree;

type
  TTrUtils=class(TObject)
  public
    class function  GetLastCode(ASelfLevel:Integer;ASelfCode,ACodeRule:string;AWithDash:Integer):string;
  public
    class procedure TreeListType(ACodeRule:string;AWithDash:Integer;AListType:TStringList;ATreeView:TElTree;ANeedCheckBox:Boolean=True);overload;
    class function  FindPrevType(ATreeView:TElTree;ASelfLevel:Integer;ASelfCode,ACodeRule:string;AWithDash:Integer):TElTreeItem;overload;

    class procedure TreeListType(ACodeRule:string;AListCord,AListType:TStringList;ATreeView:TElTree;ANeedCheckBox:Boolean=True);overload;
    class function  FindPrevType(ATreeView:TElTree;ACordIdex:Integer):TElTreeItem;overload;
  end;

implementation

uses
  Class_CORD_IN_GKZF,Class_TYPE_IN_GKZF;

{ TTrUtils }

class function TTrUtils.FindPrevType(ATreeView: TElTree;
  ACordIdex: Integer): TElTreeItem;
var
  I:Integer;
  CordA:TCord;
  ItemA:TElTreeItem;
begin
  Result:=nil;
  for I:=0 to ATreeView.Items.RootCount-1 do
  begin
    ItemA:=ATreeView.Items.RootItem[I];

    if TObject(ItemA.Data) Is TCORD then
    begin
      CordA:=TCord(ItemA.Data);
      if CordA=nil then Continue;

      if CordA.CORDIDEX=ACordIdex then
      begin
        Result:=ItemA;
        Exit;
      end;
    end;
  end;
end;

class function TTrUtils.FindPrevType(ATreeView: TElTree;
  ASelfLevel: Integer; ASelfCode, ACodeRule: string;AWithDash:Integer): TElTreeItem;
var
  I:Integer;
  TypeA:TType;
  ItemA:TElTreeItem;
begin
  Result:=nil;
  for I:=0 to ATreeView.Items.Count-1 do
  begin
    ItemA:=ATreeView.Items.Item[I];
    TypeA:=TType(ItemA.Data);
    if TypeA.TypeLevl >= ASelfLevel then Continue;
    if TypeA.TypeLink  = GetLastCode(ASelfLevel,ASelfCode,ACodeRule,AWithDash) then
    begin
      Result:=ItemA;
      Exit;
    end;
  end;
end;

class function TTrUtils.GetLastCode(ASelfLevel: Integer; ASelfCode,
  ACodeRule: string;AWithDash:Integer): string;
var
  ALength:Integer;  
begin
  Result:='';
  ALength:=StrToInt(ACodeRule[ASelfLevel]);

  if AWithDash =1 then
  begin
    Result :=Copy(ASelfCode,1,Length(ASelfCode)-ALength-1);
  end else
  begin
    Result :=Copy(ASelfCode,1,Length(ASelfCode)-ALength);
  end;
end;

class procedure TTrUtils.TreeListType(ACodeRule: string; AListCord,
  AListType: TStringList; ATreeView: TElTree; ANeedCheckBox: Boolean);
var
  I:Integer;
  CordA:TCORD;
  TypeA:TType;
  ItemA:TElTreeItem;  
begin
  ATreeView.Items.Clear;
  if (AListCord=nil) or (AListCord.Count=0) then Exit;
  if (AListType=nil) or (AListType.Count=0) then Exit;

  with ATreeView do
  begin

    if ANeedCheckBox then
    begin
      ShowCheckboxes:=True;
    end;

    Items.BeginUpdate;
    for I:=0 to AListCord.Count-1 do
    begin
      CordA:=nil;
      CordA:=TCORD(AListCord.Objects[I]);
      if CordA=nil then Continue;

      Items.AddObject(nil,CordA.CORDNAME,CordA);
    end;

    for I:=0 to AListType.Count-1 do
    begin
      TypeA:=nil;
      TypeA:=TType(AListType.Objects[I]);

      ItemA:=nil;
      if TypeA.TYPELEVL=1 then
      begin
        ItemA:=FindPrevType(ATreeView,TypeA.CORDIDEX);
      end else
      begin
        ItemA:=FindPrevType(ATreeView,TypeA.TYPELEVL,TypeA.TYPELINK,ACodeRule,1);
        if ItemA=nil then
        begin
          ItemA:=FindPrevType(ATreeView,TypeA.CORDIDEX);
        end;  
      end;

      if ANeedCheckBox then
      begin
        Items.AddChildObject(ItemA,Format('%S %S',[TypeA.TYPECODE,TypeA.TYPENAME]),TypeA).ShowCheckBox:=True;
      end else
      begin
        Items.AddChildObject(ItemA,Format('%S %S',[TypeA.TYPECODE,TypeA.TYPENAME]),TypeA);      
      end;
    end;

    FullExpand;
    
    Items.EndUpdate;
  end;
end;

class procedure TTrUtils.TreeListType(ACodeRule: string;AWithDash:Integer;
  AListType: TStringList; ATreeView: TElTree; ANeedCheckBox: Boolean);
var
  I,M  :Integer;
  TypeA:TType;
  ItemA:TElTreeItem;
  ItemB:TElTreeItem;
begin
  ATreeView.Items.Clear;

  with ATreeView do
  begin
    if ANeedCheckBox then
    begin
      ShowCheckboxes:=True;
    end;
    
    Items.BeginUpdate;

    for I:=1 to Length(ACodeRule) do
    begin
      for M:=0 to AListType.Count-1 do
      begin
        TypeA:=nil;
        TypeA:=TType(AListType.Objects[M]);
        if TypeA.TYPELEVL=I then
        begin
          ItemA:=FindPrevType(ATreeView,TypeA.TYPELEVL,TypeA.TYPELINK,ACodeRule,AWithDash);
          ItemB:=Items.AddChildObject(ItemA,Format('%s %s',[TypeA.TYPECODE,TypeA.TYPENAME]),TypeA);
          ItemB.ColumnText.Add(TypeA.TYPEMEMO);

          if ANeedCheckBox then
          begin
            ItemB.ShowCheckBox:=True;
          end;
        end;
      end;
    end;
    //FullExpand;
    FullCollapse;

    Items.EndUpdate;
  end;
end;
end.
